package com.tianbing.adlottery.controller.supplieradmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.CategoryBusiness;
import com.jubao.ejb.business.ProductBusiness;
import com.jubao.ejb.business.util.BusinessConstant;
import com.jubao.ejb.dao.BrandDao;
import com.jubao.ejb.dao.CategoryDao;
import com.jubao.ejb.dao.ProductDao;
import com.jubao.ejb.entity.Brand;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.Product;
import com.tianbing.adlottery.util.ControllerFormBean;

public class UpdateProductPrimaryCategoryController extends SimpleFormController {
	static Logger logger = Logger.getLogger(UpdateProductPrimaryCategoryController.class.getName());

	private CategoryDao categoryDao;

	private ProductDao productDao;

	private BrandDao brandDao;

	private ProductBusiness productBusiness;

	private CategoryBusiness categoryBusiness;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {
		long childCatCounts = 0;
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		Map model = mv.getModel();

		long prdsid = ServletRequestUtils.getLongParameter(request, "prdsid", -1);
		Product product = productDao.find(prdsid);
		String catid = ServletRequestUtils.getStringParameter(request, "catid", "");
		long brdsid = ServletRequestUtils.getLongParameter(request, "brdsid", -1);
		Category cat = categoryDao.find(catid);
		if (product != null && cat != null) {
			childCatCounts = categoryDao.getChildCategoryCount(cat.getSysId());
			if (childCatCounts == 0) {//only the category is leaf node
				Category oldCat = product.getPrimaryCategory();
				if (oldCat != null) {
					productBusiness.removeFromCategoryTrail(product.getSysId(), oldCat.getSysId());
				}
				product.setPrimaryCategory(cat);
				product = productDao.save(product);
				productBusiness.add2CateogryTrail(product.getSysId(), cat.getSysId());
			}
			//set brand
			Brand brand = brandDao.find(brdsid);
			if (brand != null) {
				product.setBrand(brand);
				productBusiness.add2Brand(product.getSysId(), brand.getSysId());
			}
		}
		model.put("product", product);
		model.put("prdsid", prdsid);
		model.put("catid", catid);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		long prdsid = ServletRequestUtils.getLongParameter(request, "prdsid", -1);
		String catid = ServletRequestUtils.getStringParameter(request, "catid", "");
		Product product = productDao.find(prdsid);
		if (product != null) {
			if (catid == "") {
				catid = BusinessConstant.CategoryID.PRODUCT_CATEGORY_TOP.getId();
			}

			if (!catid.equals("")) {
				Category cat = categoryDao.find(catid);

				if (cat != null) {
					model.put("category", cat);
					List primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(cat.getSysId());
					List<Category> childCategoryList = categoryDao.getChildCategoryList(cat.getSysId(), 0, 100, null);
					model.put("primaryCategoryTrail", primaryCategoryTrail);
					model.put("childCategoryList", childCategoryList);

					List<Brand> brands = categoryBusiness.getCategoryBrands(cat.getSysId());
					model.put("brandList", brands);
				}
			}

			Category cat = product.getPrimaryCategory();
			if (cat != null) {
				List<Category> trail = categoryDao.getPrimaryCategoryTrail(cat.getSysId());
				model.put("oldPrimaryCategoryTrail", trail);
			}
			model.put("product", product);
		}
		return model;
	}

	/**
	 * @param logger the logger to set
	 */
	public static void setLogger(Logger logger) {
		UpdateProductPrimaryCategoryController.logger = logger;
	}

	/**
	 * @param categoryDao the categoryDao to set
	 */
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public void setBrandDao(BrandDao brandDao) {
		this.brandDao = brandDao;
	}

	public void setProductBusiness(ProductBusiness productBusiness) {
		this.productBusiness = productBusiness;
	}

	public void setCategoryBusiness(CategoryBusiness categoryBusiness) {
		this.categoryBusiness = categoryBusiness;
	}

}
